<template>
  <el-container class="home_contain">
    <!-- 头部区域 -->
    <el-header
      ><div>
        <img src="../assets/头像.png" class="ava_img" alt="" /><span
          >电商后台管理系统</span
        >
      </div>
      <el-button type="info" @click="loginout">退出</el-button></el-header
    >
    <!-- 页面主体区 -->
    <el-container>
      <!-- 左侧区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333644"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单区域 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="subItem in item.children"
              :index="'/' + subItem.path"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template></el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体区 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      // 图标对象
      iconObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-suitcase-1',
        101: 'el-icon-s-cooperation',
        102: 'el-icon-collection-tag',
        145: 'el-icon-full-screen'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    //   为退出按钮绑定事件
    loginout() {
      window.sessionStorage.clear();
      this.$router.push('/login');
    },
    // 获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus');
      if (res.meta.status !== 200) return this.$message.err(res.meta.msg);
      this.menulist = res.data;
      // console.log(res);
    },
    // 点击按钮，实现导航栏的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath);
      this.activePath = activePath;
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  background-color: rgb(55, 60, 64);
  display: flex;
  justify-content: space-between;
  padding: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
  }
}
.el-aside {
  background-color: #333644;
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background-color: #eaedf1;
}
.home_contain {
  height: 100%;
}
.ava_img {
  width: 70px;
  height: 40px;
}
.toggle-button {
  background-color: #495066;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>